{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>添加优惠券</h5>
                </div>
                <div class="ibox-content layui-form">
                    <div class="layui-row layui-col-space10 layui-form-item ">

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">优惠券名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_name" lay-verify="required" placeholder="优惠券名称" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">描述：</label>
                            <div class="layui-input-block">
                                <textarea name="coupon_intro" lay-verify="required" placeholder="描述" class="layui-textarea"></textarea>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">发放数量：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_total_num" lay-verify="required" placeholder="发放数量，0代表不限制" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 form-group" id="coupon_type">
                            <label class="layui-form-label">使用条件：</label>
                            <div class="layui-input-block">
                                <select name="coupon_type"  lay-filter="role1">
                                    <option value="2" selected>无门槛</option>
                                    <option value="1" >满减</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 hidden" id="use_price">
                            <label class="layui-form-label">使用金额：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_use_price" placeholder="使用金额" class="layui-input" oninput="value=value.replace(/[^\d.]/g,'')">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">面值：</label>
                            <div class="layui-input-block">
                                <input type="text" name="coupon_dikou_price" lay-verify="required" placeholder="面值， 0.01~10000元之间" class="layui-input" oninput="value=value.replace(/[^\d.]/g,'')">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">使用开始日期：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="coupon_use_start_time" lay-verify="required" placeholder="使用开始日期" class="layui-input test-item">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">使用结束日期：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="coupon_use_end_time" lay-verify="required" placeholder="使用结束日期" class="layui-input test-item">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 form-group" id="coupon_range">
                            <label class="layui-form-label">发送范围：</label>
                            <div class="layui-input-block">
                                <select name="coupon_range"  lay-filter="role2">
                                    <option value="2" selected>全部用户</option>
                                    <option value="1" >部分用户</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8 hidden" id="user_id">
                            <label class="layui-form-label">用户：</label>
                            <div class="layui-input-block">
                                <select name="coupon_user_ids"  xm-select="select" xm-select-search="" xm-select-search-type="dl">
                                    {foreach name="user" item="vo"}
                                    <option value="{$vo.user_id}" >{$vo.user_username}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <label class="layui-form-label">发送时间：</label>
                            <div class="layui-input-inline">
                                <input type="text" name="coupon_send_time" lay-verify="required" placeholder="发送时间" class="layui-input test-item1">
                            </div>
                        </div>

                        <div class="layui-form-item layui-col-md-offset1 layui-col-md8">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">发布
                                </button>
                                <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">关闭</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    var coupon_type = 2; // 优惠券类型
    layui.use(['form','upload','layedit','laydate'], function() {
        var form = layui.form;
        form.on('submit(component-form-element)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            if(coupon_type == 2){
                layer.confirm('无门槛优惠券可能会造成资金流失', {icon: 3, title:'提示'}, function(index){
                    $.ajax({
                        url:"{:url('couponAdd')}",
                        type:'post',
                        dataType:'json',
                        data:data.field,
                        success:function(res){
                            if (res.code == 200) {
                                wk.success(res.msg,"{:url('Coupon/index')}")
                            } else {
                                wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                            }
                        }
                    });
                });
            }else {
                $.ajax({
                    url:"{:url('couponAdd')}",
                    type:'post',
                    dataType:'json',
                    data:data.field,
                    success:function(res){
                        if (res.code == 200) {
                            wk.success(res.msg,"{:url('Coupon/index')}")
                        } else {
                            wk.error(res.msg,'','$(".layui-btn").removeClass(\'layui-disabled\').removeAttr(\'disabled\');');
                        }
                    }
                });
            }
        });

        form.on('select(role1)', function(data){
            coupon_type = data.value;
            if(data.value == 1){
                $("#use_price").removeClass('hidden');
            }else{
                $("#use_price").addClass('hidden');
            }
        });
        form.on('select(role2)', function(data){
            coupon_range = data.value;
            if(data.value == 1){
                $("#user_id").removeClass('hidden');
            }else{
                $("#user_id").addClass('hidden');
            }
        });
        var laydate = layui.laydate;
        //同时绑定多个
        lay('.test-item').each(function(){
            laydate.render({
                elem: this
                ,format:'yyyy-MM-dd'
                ,type:'date'
                ,trigger: 'click'
                ,min:getNowFormatDate()
            });
        });
        lay('.test-item1').each(function(){
            laydate.render({
                elem: this
                ,format:'yyyy-MM-dd HH:mm'
                ,type:'datetime'
                ,trigger: 'click'
                ,min:getNowFormatDate()
            });
        });

        function getNowFormatDate() {
            var date = new Date( );
            var seperator1 = "-";
            var seperator2 = ":";
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                    + " " + date.getHours() + seperator2 + date.getMinutes()
                    + seperator2 + date.getSeconds();
            return currentdate;
        }
    });
</script>

</body>
</html>
